package com.shasselclub.project.ask.problem.controller;

import java.util.List;

import com.shasselclub.common.utils.StringUtils;
import com.shasselclub.project.ask.question.domain.Question;
import com.shasselclub.project.ask.question.service.IQuestionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shasselclub.framework.aspectj.lang.annotation.Log;
import com.shasselclub.framework.aspectj.lang.enums.BusinessType;
import com.shasselclub.project.ask.problem.domain.Problem;
import com.shasselclub.project.ask.problem.service.IProblemService;
import com.shasselclub.framework.web.controller.BaseController;
import com.shasselclub.framework.web.domain.AjaxResult;
import com.shasselclub.common.utils.poi.ExcelUtil;
import com.shasselclub.framework.web.page.TableDataInfo;

/**
 * 问题管理Controller
 * 
 * @author 郑云飞
 * @date 2022-08-05
 */
@Controller
@RequestMapping("/ask/problem")
public class ProblemController extends BaseController
{
    private String prefix = "ask/problem";

    @Autowired
    private IProblemService problemService;

    @Autowired
    private IQuestionService questionService;
    @RequiresPermissions("ask:problem:view")
    @GetMapping()
    public String problem()
    {
        return prefix + "/problem";
    }

    /**
     * 查询问题管理列表
     */
    @RequiresPermissions("ask:problem:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Problem problem)
    {
        startPage();
        List<Problem> list = problemService.selectProblemList(problem);
        for(int i=0;i<list.size();i++){
            long questionId=list.get(i).getquestionId();
            Question question=questionService.selectQuestionById(questionId);
            if(StringUtils.isNotNull(question)){
                String questionName=question.getTitle();
                list.get(i).setQuetionName(questionName);
            }

        }
        return getDataTable(list);
    }

    /**
     * 导出问题管理列表
     */
    @RequiresPermissions("ask:problem:export")
    @Log(title = "问题管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Problem problem)
    {
        List<Problem> list = problemService.selectProblemList(problem);
        ExcelUtil<Problem> util = new ExcelUtil<Problem>(Problem.class);
        return util.exportExcel(list, "问题管理数据");
    }

    /**
     * 新增问题管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存问题管理
     */
    @RequiresPermissions("ask:problem:add")
    @Log(title = "问题管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Problem problem)
    {
        return toAjax(problemService.insertProblem(problem));
    }

    /**
     * 修改问题管理
     */
    @RequiresPermissions("ask:problem:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        Problem problem = problemService.selectProblemById(id);
        mmap.put("problem", problem);
        return prefix + "/edit";
    }

    /**
     * 修改保存问题管理
     */
    @RequiresPermissions("ask:problem:edit")
    @Log(title = "问题管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Problem problem)
    {
        return toAjax(problemService.updateProblem(problem));
    }

    /**
     * 删除问题管理
     */
    @RequiresPermissions("ask:problem:remove")
    @Log(title = "问题管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(problemService.deleteProblemByIds(ids));
    }
}
